Redis客户端:C/C++ hiredis
摘要1.Linux下安装hiredis2.Windows下安装hiredis3.其他C/C++的Redis客户端
摘要
本篇博客对Redis基于C/C++的客户端工具hiredis进行简单介绍,以便加深理解和记忆
1.Linux下安装hiredis
1)方式一:apt
# 查询c语言对应的开发库
apt-cache search hiredist
# 下载安装
sudo apt-get install libhiredis-dev
hiredis库目录的默认位置为/usr/lib/x86_64-linux-gnu/下,头文件在/usr/include/hiredis中,hiredis头文件中定义了各种API
2)方式二:源码编译
下载hiredis源码
# 解压后编译
make
sudo make install
2.Windows下安装hiredis
下载微软发布的windows下的redis源码(在官网下载发布版的zip包更稳定)
git clone https://github.com/microsoftarchive/redis.git
用新版的Visual Studio打开工程
打开时对该项目之前使用的Visual Studio工具集进行版本升级,以适配当前的版本的Visual Studio
编译Redis源码
错误修复:编译过程中会报system_error:不是"std"的成员变量、system_error:找不到标识符、system_category:找不到标识符
去掉std::、在报找不到标识符的文件中引入#include 头文件
重新编译:成功
将编译后的静态链接文件导入到项目的libs文件夹下,并在Visual Studio中增加链接
将redis\deps\hiredis和redis\src\Win32_Interop目录放入项目的include文件夹中(或与项目源文件同级目录下)并进行适配
启动redis-server.exe服务端,并进行连接测试
// TestRedis.cpp : 定义控制台应用程序的入口点。
#include
#include
#include
#include
//#include
int main() {
unsigned int j;
redisContext* c;
redisReply* reply;
c = redisConnect("127.0.0.1", 6379);
if (c == NULL || c->err) {
if (c) {
printf("Connection error: %s\n", c->errstr);
redisFree(c);
}
else {
printf("Connection error: can't allocate redis context\n");
}
exit(1);
}
else {
printf("connect redis successed!\n");
}
/* PING server */
reply = (redisReply*)redisCommand(c, "PING");
printf("PING: %s\n", reply->str);
freeReplyObject(reply);
/* Set a key */
reply = (redisReply*)redisCommand(c, "SET %s %s", "foo", "hello world");
printf("SET: %s\n", reply->str);
freeReplyObject(reply);
/* Set a key using binary safe API */
reply = (redisReply*)redisCommand(c, "SET %b %b", "bar", (size_t)3, "hello", (size_t)5);
printf("SET (binary API): %s\n", reply->str);
freeReplyObject(reply);
/* Try a GET and two INCR */
reply = (redisReply*)redisCommand(c, "GET foo");
printf("GET foo: %s\n", reply->str);
freeReplyObject(reply);
for (j = 0; j
char buf[64];
snprintf(buf, 64, "%u", j);
reply = (redisReply*)redisCommand(c, "LPUSH mylist element-%s", buf);
freeReplyObject(reply);
}
/* Let's check what we have inside the list */
reply = (redisReply*)redisCommand(c, "LRANGE mylist 0 -1");
if (reply->type == REDIS_REPLY_ARRAY) {
for (j = 0; j elements; j++) {
printf("%u) %s\n", j, reply->element[j]->str);
}
}
freeReplyObject(reply);
/* Disconnects and frees the context */
redisFree(c);
system("pause");
return 0;
}
3.其他C/C++的Redis客户端
客户端最后更新支持Linux支持Windows支持pub/sub支持redis版本备注C++ Client2011-03-03√×2.0c+redis+client2016-09-30√自加工√A redis client based on hiredis, supports cluster/pipeline and is thread safe and includes two files only. The transaction is on the way )cpp_redis2016-10-26√√C++11 Lightweight Redis client: async, thread-safe, no dependency, pipelining, multi-platform.qredisclient2016-11-04√Asynchronous Qt-based Redis client with SSL and SSH tunnelling support.r3c2016-11-11√×A Redis Cluster C++ Client, based on hiredis and support standalone, it’s easy to make and use, not depends on C++11 or later.redis-client for C++2016-11-07√√√Full Redis client commands, one redis command, one redis functionredis3m2015-10-20√×A C++ wrapper of hiredis, with also connection pooling, high availability and ready-to-use patternsredisclient2016-10-12√√√A C++ asynchronous client based on boost::asioredox2016-07-29√×√Modern, asynchronous, and fast C++11 client for RedisSimpleRedisClient2015-03-14√×2.6/2.8a very simple Redis client for C++xredis2016-10-14√√√Redis C++ client with data slice storage, connection pool, master slave connection, read/write separation; requires hiredis only
|